package com.sduept.nwld.dataserver.manager.etl;

import com.sduept.core.dao.SuperDAO;
import com.sduept.generator.ObjectId;
import com.sduept.bigdata.fault.common.ETLDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class BxBusbarsectionETLManager {

	@Autowired
	private SuperDAO dao;
	
	@Autowired
	private ETLDAO eDao;
	
	public  void receiveAndSaveAllData() {
		List<?> list = getAllSourceData();
		receiveAndSave(list);
	}
	public  void receiveAndSavePartData(String date) {
		List<?> list = getAllSourceData(date);
		receiveAndSave(list);
	}
	
	
	public List<?> getAllSourceData(){
		List <?>result = eDao.query("select cim_mrid,cim_mid,cim_mname,cim_mstationid,cim_mvoltage,cim_stationid from t_bx_busbarsection ");
		return result;
	}
	public List<?> getAllSourceData(String date) {
		String sql = "select cim_mrid,cim_mid,cim_mname,cim_mstationid,cim_mvoltage,cim_stationid from t_bx_busbarsection "
				+ " where update_time=";
		List<?> result = eDao.query(sql + "'" + date + "'");
		return result;
	}
	public  void receiveAndSave(List<?> list) {
		ETLDataSave equipmentSave = new ETLDataSave(list);
		equipmentSave.saveToBean();
	}
	public class ETLDataSave{
		private List<?> list;
 
		public ETLDataSave(List<?> list) {
			super();
			this.list = list;
		}
		public void saveToBean(){
			String sql="insert into t_bx_busbarsection(cim_mrid,cim_mid,cim_mname,cim_mstationid,cim_mvoltage,cim_stationid,id) values(?,?,?,?,?,?,?)";
			 List<Object[]> datas = new ArrayList<Object[]>();
			for(Object obj : list){
				Object[] objs = (Object[]) obj;
				Object[] saveObjs=new Object[7];
				for(int i=0;i<objs.length;i++){
					if(objs[i]!=null&&"null".equals(objs[i].toString())){
						objs[i]=null;
					}
					saveObjs[i]=objs[i];

				}
				saveObjs[6]=ObjectId.get().toString();
				datas.add(saveObjs);
			}
			try {
				dao.persistentBatch(sql, datas);
				log.info("更新保信母线："+datas.size());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}
